package com.fw.payment.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fw.payment.bao.MerchantTransactionDataBAO;
import com.fw.payment.bao.ServiceProfileBAO;
import com.fw.service.inboundmessagelog.bean.InboundMessageLogBean;
import com.fw.service.merchanttransactiondata.bean.MerchantTransactionDataBean;
import com.fw.service.serviceprofile.bean.ServiceProfileBean;

@Controller
@RequestMapping(value = "/")
public class PayAtAllResponseController extends ReponseBackgroundController {

    private static final Logger log = Logger.getLogger(PayAtAllResponseController.class);

    public static final String REQUEST_PARAM_INVOICENO = "invoiceNo";
    public static final String MESSAGE = "message";
    public static final String MESSAGE_SUCCESS = "S";
    public static final String MESSAGE_FAIL = "F";

    public static final String ACTION_PAYATALL_CASH_BACK = "/payatall/cash/back";
    public static final String ACTION_PAYATALL_CASH_CANCEL = "/payatall/cash/cancel";
    public static final String ACTION_PAYATALL_CASH_CONFIRM = "/payatall/cash/confirm";

    private static final Logger logger = Logger.getLogger(PayAtAllResponseController.class);

    @Autowired
    private ServiceProfileBAO serviceProfileBAO;

    @Autowired
    private MerchantTransactionDataBAO merchantTransactionDataBAO;

    @RequestMapping(value = PayAtAllResponseController.ACTION_PAYATALL_CASH_BACK, method = RequestMethod.GET)
    public String cashBack(@RequestParam(value = PayAtAllResponseController.REQUEST_PARAM_INVOICENO, required = true) String invoiceNo,
	    HttpServletRequest request, Locale locale) {
	logger.info("reponse back from cash");
	InboundMessageLogBean inboundMessageLogBean = null;
	ServiceProfileBean serviceProfileBean = null;
	try {
	    MerchantTransactionDataBean merchantTransactionDataBean = merchantTransactionDataBAO
		    .getMerchantTransactionDataByInvoiceNo(invoiceNo);
	    if (merchantTransactionDataBean == null) {
		return returnView("error", inboundMessageLogBean, serviceProfileBean);
	    }
	    inboundMessageLogBean = merchantTransactionDataBean.getInboundMessageLogBean();
	    if (inboundMessageLogBean != null) {
		serviceProfileBean = serviceProfileBAO.getServiceProfileByServiceId(inboundMessageLogBean.getServiceID());
	    }
	    merchantTransactionDataBean.setPaymentStatus(MerchantTransactionDataBean.PAYMENT_STATUS_PENDING);
	    merchantTransactionDataBAO.updateMerchantTransactionData(merchantTransactionDataBean);
	} catch (Exception e) {
	    log.error(e, e);
	    return returnView("error", inboundMessageLogBean, serviceProfileBean);
	}
	return returnView("payatall/cash/back", inboundMessageLogBean, serviceProfileBean, ReponseBackgroundController.REDIRECT_SUCCESS);
    }

    @RequestMapping(value = PayAtAllResponseController.ACTION_PAYATALL_CASH_CANCEL, method = RequestMethod.GET)
    public String cashCancel(@RequestParam(value = PayAtAllResponseController.REQUEST_PARAM_INVOICENO, required = true) String invoiceNo,
	    HttpServletRequest request, Locale locale) {
	logger.info("reponse cancel from cash");
	InboundMessageLogBean inboundMessageLogBean = null;
	ServiceProfileBean serviceProfileBean = null;
	try {
	    MerchantTransactionDataBean merchantTransactionDataBean = merchantTransactionDataBAO
		    .getMerchantTransactionDataByInvoiceNo(invoiceNo);
	    if (merchantTransactionDataBean == null) {
		return returnView("error", inboundMessageLogBean, serviceProfileBean);
	    }
	    inboundMessageLogBean = merchantTransactionDataBean.getInboundMessageLogBean();
	    if (inboundMessageLogBean != null) {
		serviceProfileBean = serviceProfileBAO.getServiceProfileByServiceId(inboundMessageLogBean.getServiceID());
	    }
	    merchantTransactionDataBean.setPaymentStatus(MerchantTransactionDataBean.PAYMENT_STATUS_FAILED);
	    merchantTransactionDataBAO.updateMerchantTransactionData(merchantTransactionDataBean);
	} catch (Exception e) {
	    log.error(e, e);
	    return returnView("error", inboundMessageLogBean, serviceProfileBean);
	}
	return returnView("payatall/cash/cancel", inboundMessageLogBean, serviceProfileBean, ReponseBackgroundController.REDIRECT_CANCEL);
    }

    @RequestMapping(value = PayAtAllResponseController.ACTION_PAYATALL_CASH_CONFIRM, method = RequestMethod.POST)
    public String cashConfirm(@RequestParam(value = "mid", required = false) String mid,
	    @RequestParam(value = "inv", required = false) String inv,
	    @RequestParam(value = "serviceid", required = false) String serviceid,
	    @RequestParam(value = "ptransid", required = false) String ptransid,
	    @RequestParam(value = "status", required = false) String status,
	    @RequestParam(value = "payment_date", required = false) String paymentDate,
	    @RequestParam(value = "process_date", required = false) String processDate, HttpServletRequest request, Locale locale,
	    ModelMap model) {
	logger.info("aeon online payment channel payment (POST)");
	return "payatall/cash/confirm";
    }

}
